package com.apcc4m.sfpushbatch.config;

import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import com.apcc4m.sfpushbatch.service.BatchManagerService;

@Configuration
@PropertySource(value = "classpath:batch.properties")
public class SfpushSchedulConfig {

    @Autowired
    private BatchManagerService batchManager;

    @Value("${batch.targetMethod}")
    private String targetMethod;

    @Value("${batch.cronExpression}")
    private String cronExpression;

    @Bean
    public MethodInvokingJobDetailFactoryBean jobDetail() {
        MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean();
        bean.setTargetObject(batchManager);
        bean.setTargetMethod(targetMethod);
        return bean;
    }

    @Bean
    public CronTriggerFactoryBean jobTrigger() {
        CronTriggerFactoryBean bean = new CronTriggerFactoryBean();
        bean.setJobDetail(jobDetail().getObject());
        bean.setCronExpression(cronExpression);
        return bean;
    }

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() {
        SchedulerFactoryBean bean = new SchedulerFactoryBean();
        Trigger[] triggers = { jobTrigger().getObject() };
        bean.setTriggers(triggers);
        return bean;
    }

}
